function gadget:GetInfo()
return {
		name = "subsystems.lua",
		desc = "adds gather command",
		author = "tomr13",
		date = "10/11/2011",
		license = "public domain",
		layer = 1,
		enabled = true,
}
end

local workerTypes={}
workerTypes[UnitDefNames["gatherer"].id]={
  capacity=5,
  gatherTime=1,
}

local hqTypes={}

hqTypes[UnitDefNames["simplefactory"].id]={
  returnRange=60
}
--endconfig

local Ships = {
	[UnitDefNames.gatherer.id] = "gatherer",
	[UnitDefNames.heavyattackship.id] = "heavyattackship",
	[UnitDefNames.lightattackship.id] = "lightattackship",
}

local CMD_MAX_DAMAGE_SUBSYSTEM = 33631

local echo = Spring.Echo

if not GG.attUnits then
	GG.attUnits = {}
end

local units = {}

Damage_Subsystem_Toggle = {
		id=CMD_MAX_DAMAGE_SUBSYSTEM,
		type=CMDTYPE.ICON,
			name="Damage",
			texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/burrow.png",
			tooltip="Gathers wreckage.",
			action="damagesubsystem"
		}
		
if (not gadgetHandler:IsSyncedCode()) then 
 return false
end

local function ChangeSubSystems(unitID, health, movement, damage)
	--Spring.Echo("HELLO")
	Spring.SetUnitRulesParam(unitID,"Health_Subsystem", health)
	
	Spring.SetUnitRulesParam(unitID,"Movement_Subsystem", movement)
	Spring.SetUnitRulesParam(unitID,"Damage_Subsystem", damage)
	
	local a = Spring.GetUnitRulesParam(unitID,"Health_Subsystem")
	local b = Spring.GetUnitRulesParam(unitID,"Movement_Subsystem")
	local c = Spring.GetUnitRulesParam(unitID,"Damage_Subsystem")
	--Spring.Echo("Subsystems Set: " .. a .. "00" .. b .. "000" .. c)
end

function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
 if (cmdID~=CMD_MAX_DAMAGE_SUBSYSTEM) then
  return false
 end
 if(not Ships[unitDefID]) then
   return true, true --this shouldn't happen and I'm not sure but I think that this removes the command from the list
 end
 ChangeSubSystems(unitID,0,0,100)
end

function gadget:RecvLuaMsg(msg, playerID)

	local loc1 = string.find(msg, ",", 1);
	local loc2 = string.find(msg, ",", loc1+1);
	local loc3 = string.find(msg, ",", loc2+1);
	local a = string.sub(msg,1,loc1-1)
	local b = string.sub(msg,loc1+1,loc2-1)
	local c = string.sub(msg,loc2+1,loc3-1)
	c =  math.floor(c*100)
	
	if(tonumber(a) == 1) then
		local a = Spring.GetUnitRulesParam(b,"Health_Subsystem", c)
	end
	if(tonumber(a) == 2) then
		local a = Spring.GetUnitRulesParam(b,"Movement_Subsystem", c)
	end
	if(tonumber(a) == 3) then
		local a = Spring.GetUnitRulesParam(b,"Damage_Subsystem", c)
	end
end

function gadget:UnitPreDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)
   if(Spring.GetUnitRulesParam(attackerID,"Damage_Subsystem")) then
		--echo(" Attacker ID: " .. attackerID .." DAMAGE" .. Spring.GetUnitRulesParam(attackerID,"Damage_Subsystem"))
		if(Spring.GetUnitRulesParam(attackerID,"Damage_Subsystem") > 0) then
			return damage * Spring.GetUnitRulesParam(attackerID,"Damage_Subsystem") / 100.0
		end
	end
	return damage
end

function gadget:UnitDestroyed(unitID, unitDefID, team)
	if(Ships[unitDefID]) then
		table.remove(units,unitID)
	end
end

function gadget:UnitCreated(unitID, unitDefID, unitTeam)
	if(Ships[unitDefID]) then
		Spring.InsertUnitCmdDesc(unitID, Damage_Subsystem_Toggle)
		table.insert(units,unitID)
	end
end

function gadget:Initialize()
 gadgetHandler:RegisterCMDID(CMD_MAX_DAMAGE_SUBSYSTEM)
end